Ext.define('WebOs.Kernel.UserMgr', {

	activeUser : null,

	moduleStore : null,
	
	serviceStore : null,
	
	moduleStoreLoaded : false,
	
	actionStoreLoaded : false,
	
	mixins : {
		observable : 'Ext.util.Observable'
	},
	
	constructor : function(config) {
		var me = this;
		me.mixins.observable.constructor.call(this, config);
		me.addEvents('moduleloaded','actionloaded','userserviceloaded');
		Ext.apply(me, config);
		this.serviceStore = Ext.create('Ext.data.Store', {
					model : 'WebOs.Kernel.model.FileModel',
					data : []
				});
		me.addListener('moduleloaded',function(result){
			this.moduleStore = Ext.create('Ext.data.Store', {
					model : 'WebOs.Kernel.model.FileModel',
					data : result
				});
			this.serviceStore.add(result);
			me.moduleStoreLoaded = true;
			if(me.actionStoreLoaded){
				me.fireEvent('userserviceloaded',me.serviceStore);
			}
		});
		me.addListener('actionloaded',function(result){
			this.serviceStore.add(result);
			me.actionStoreLoaded = true;
			if(me.moduleStoreLoaded){
				me.fireEvent('userserviceloaded',me.serviceStore);
			}
		});
	},
	/**
	 * 登陆函数： 若登陆成功则返回该用户对象 登陆失败则返回null
	 * 
	 * @param {}
	 *            name
	 * @param {}
	 *            pass
	 * @param {}
	 *            error 登陆错误错误提示信息
	 */
	login : function(name, pass,vcode, error) {
		var me = this;
		Ext.Ajax.request({
					url : Constants.REQUEST_URL,
					params : {
						action : 'login',
						module : 'platform',
						userName : name,
						password : pass,
						verifyCode : vcode
					},
					method : 'POST',
					success : function(resp) {
						var response = Ext.decode(resp.responseText);
						if (response['code'] != 0) {
							error.call(core, response['msg']);
						} else {
							var user = response['result'];
							user['name'] = user['userName'];
							user['text'] = user['realName'];
							user['time'] = new Date();
							me.activeUser = new WebOs.Kernel.model.UserModel(user);
							core.fireEvent('userlogin', me.activeUser);
						}
					},
					failure : function(resp) {
						error.call(core, '系统内部发生异常');
					}
				});
		return me.activeUser;
	},

	logout : function(fn) {
		Ext.Ajax.request({
					url : Constants.REQUEST_URL,
					params : {
						action : 'logout',
						module : 'platform'
					},
					method : 'POST',
					success : function(resp) {
						if(fn && typeof fn == 'function'){
							fn.call();
						}	
					},
					failure : function(resp) {
						
					}
				});
	},

	checkLogin : function(yes, no) {
		var me = this;
		Ext.Ajax.request({
					url : Constants.REQUEST_URL,
					params : {
						action : 'checkLogin',
						module : 'platform'
					},
					method : 'POST',
					success : function(resp) {
						var user = Ext.decode(resp.responseText).result;
						if(user){
							user['name'] = user['userName'];
							user['text'] = user['realName'];
							user['time'] = new Date();
							me.activeUser = new WebOs.Kernel.model.UserModel(user);
							yes.call(core,me.activeUser);
						}else{
							no.call(core);
						}
					},
					failure : function(resp) {
						no.call(core);
					}
				})
	},
	/**
	 * 1.获取所有service ,包含module和action 2.过滤除module的service 赋值给serviceStore
	 * 
	 * @return {}
	 */
	getUserService : function() {
		this.getModuleService();
		this.getActionService();
	},

	getModuleService : function() {
		var me = this;
		var result = [];
		Ext.Ajax.request({
					url : Constants.REQUEST_URL,
					params : {
						action : 'getUserPerModules',
						module : 'platform'
					},
					method : 'POST',
					success : function(resp) {
						var response = Ext.decode(resp.responseText);
						for (var p in response['result']) {
							var module = response['result'][p];
							module['name'] = module['moduleName'];
							module['text'] = module['moduleText'];
							module['serviceType'] = module['moduleType'];
							delete module.id;
							result
									.push(new WebOs.Kernel.model.FileModel(module));
						}
						me.fireEvent('moduleloaded',result);	
					},
					failure : function(resp) {
						me.fireEvent('moduleloaded',result);
						console.log('[WARN]','no module loaded');
					}
				});
	},

	getActionService : function() {
		var me = this;
		var result = [];
		Ext.Ajax.request({
					url : Constants.REQUEST_URL,
					params : {
						action : 'getUserInitPermission',
						module : 'platform'
					},
					method : 'POST',
					success : function(resp) {
						var response = Ext.decode(resp.responseText);
						for (var p in response['result']) {
							var action = response['result'][p];
							var module = action.split(':')[0];
							result.push(new WebOs.Kernel.model.FileModel({
										name : action,
										pname : module,
										serviceType : 3
									}));
						}
						me.fireEvent('actionloaded',result);	
					},
					failure : function(resp) {
						me.fireEvent('actionloaded',result);
						console.log('[WARN]','no action loaded');
					}
				});
	},

	getActiveUser : function() {
		return this.activeUser;
	},

	getActiveUserName : function() {
		if (this.activeUser) {
			return this.activeUser.get('name');
		} else {
			return '匿名'
		}
	},

	setUserLocalData : function(key, str) {
		// setCookie(this.getActiveUserName() + '@' + key, Ext
		// .encode(jsonData));
		setCookie(this.getActiveUserName() + '@' + key, str);
	},

	getUserLocalData : function(key) {
		var localData = getCookie(this.getActiveUserName() + '@' + key);
		// return Ext.decode(localData);
		return localData;
	}
});